package cn.yoaket.unit;

import com.alibaba.fastjson2.annotation.JSONField;

import java.util.HashSet;
import java.util.Objects;

import java.util.*;

public class Point implements Comparable<Point> {

    double x;
    double y;

    @JSONField(serialize = false)
    int id;

    @JSONField(serialize = false)
    public Point pre;

    @JSONField(serialize = false)
    public Set<Point> near;
    //预估代价
    @JSONField(serialize = false)
    double cost;
    //已经走过的步长
    @JSONField(serialize = false)
    public double steps;

    public double getCost() {
        return cost;
    }

    public void setCost(double cost) {
        this.cost = cost;
    }

    static final double epsilon = 10E-10;

    public Point() {
        steps = 0;
        this.cost = 0;
        this.id = -1;
        near = new HashSet<>();
    }

    public Point(double x, double y) {
        this.x = x;
        this.y = y;
        steps = 0;
        this.cost = 0;
        this.id = -1;
        near = new HashSet<>();
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "[" + x + "," + y + "]";
    }

    public void add(Point p) {
        this.near.add(p);
    }

    public double getX() {
        return x;
    }

    public double getY() {
        return y;
    }

    public void setLocation(double x, double y) {
        this.x = x;
        this.y = y;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Point point = (Point) o;
        return Math.abs(this.x - point.x) < epsilon && Math.abs(this.y - point.y) < epsilon;
    }

    @Override
    public int hashCode() {
        return Objects.hash(x, y);
    }

    @Override
    public int compareTo(Point p) {
        if (this.getCost() - p.getCost() == 0)
            return 0;
        else if (this.getCost() - p.getCost() > 0)
            return 1;
        else
            return -1;
    }
}
